#ifndef CWGENERATOR_H
#define CWGENERATOR_H

#include "cwgen_global.h"

#include <QObject>
#include <QStringList>
#include <ao/ao.h>
#include <math.h>
#include <QDebug>
#include <QHash>
#include <QFile>

class CWGenerator : public QObject
{
    Q_OBJECT
public:
    enum ToneType{
        TT_SINUS,
        TT_MEANDR
    };

    explicit CWGenerator(QObject *parent = 0);
    ~CWGenerator();

    bool loadCode(const QString &file);
    
signals:
    void endTextGen();
    void startTextGen();
    void pauseTextGen(int pos);
    
public slots:
    void setCWFreq(double freq);
    void setCWSpeed(int sign_per_sec);
    void setCWSpeedWPM(int wpm);
    void setDashDuration(int dots);
    void setSignPause(int dots);
    void setWordPause(int dots);
    void setVolume(double volume);
    void genDot();
    void genDash();
    void genCWText(const QString &text, int pos = 0);
    void setToneType(ToneType type);
    void genTone(double freq, double time, double volume = 0.75);
    void stop();
    void pause();
    
protected:
    void aoInit();
    void aoShutdown();
    void genSinus(double freq, double time, double volume = 0.75);
    void genMeandr(double freq, double time, double volume = 0.75);

private:
    ao_device *dev;
    int driver;
    ao_sample_format fmt;
    ToneType ttype;
    double tone_freq;
    double tone_volume;

    double dot_t; //dots duration
    int dash_dot; //dots in the dash
    int sign_pause; //dots in the pause between signs
    int word_pause; //dots in the pause between words
    bool _stop_flag;
    int cur_pos;

    QHash<QString,QString> cwmap;
};

#endif // CWGENERATOR_H
